package com.zrspring.libv2.base;
/*   
 * Copyright (c) 2014-2014 China.ChongQing.MaiQuan Ltd All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * @ClassName: BasicAdapter
 * @Description: 简要封装baseAdapter，减少重复代码
 * @author 请填写作者名
 * @date 2014-5-24 下午11:46:30
 * @ConputerUserName zrspring
 * @param <E>
 *            数据类型
 * @param <H>
 *            viewholder
 */
public abstract class AdapterZr<E,H> extends BaseAdapter {

	/**
	 * @Fields _datas : 数据源
	 */
	private List<E> _datas;
	/**
	 * @Fields _mLayout : 布局文件
	 */
	private int _mLayout;
	/**
	 * @Fields mInflater : 界面inflater
	 */
	private LayoutInflater _inflater;
	/**
	 * @Fields _context : 上下文
	 */
	private Context _context;
	/**
	 * @Fields mLock : 对象锁，只允许单线程操作列表数据
	 */
	private final Object mLock = new Object();

	public AdapterZr(Context context, List<E> listDatas) {
		super();
		this._datas = listDatas;
		this._context = context;
		this._mLayout = setLayout();
		this._inflater = LayoutInflater.from(_context);
	}

	@Override
	public int getCount() {
		return _datas != null ? _datas.size() : 0;
	}

	@Override
	public E getItem(int position) {
		return _datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View _view = convertView;
		H holder;
		if (_view == null) {
			_view = _inflater.inflate(_mLayout, null);
			holder = createHolder();
			initHolder(_view, holder);
			_view.setTag(holder);
		} else {
			holder = (H) _view.getTag();
		}
		//T item = getItem(position);
		bundleValue(holder,getItem(position),position);
		return _view;
	}

	/**
	* @Title: setLayout
	* @Description: 设置listview的布局文件  
	* @return 
	* @throws
	*/
	protected abstract int setLayout();
	
	/**
	* @Title: createHolder
	* @Description: 创建ViewHolder  
	* @return  new ViewHolder
	* @throws
	*/
	protected abstract H createHolder();

	/**
	* @Title: initHolder
	* @Description: 初始化ViewHolder值  
	* @param convertView
	* @param holder 
	* @throws
	*/
	protected abstract void initHolder(View convertView,H holder);

	/**
	* @Title: bundleValue
	* @Description: 绑定Holder中控件的值
	* @param holder 
	* @param item 
	* @param position 
	* @throws
	*/
	protected abstract void bundleValue(H holder, E item,int position);

	/**
	 * @Title: getPosition
	 * @Description: 获取当前 item 在list中的位置
	 * @param item
	 * @return
	 * @throws
	 */
	public int getPosition(E item) {
		return _datas.indexOf(item);
	}

	/**
	 * @Title: add
	 * @Description: 添加当前数据
	 *            单个对象数据
	 * @throws
	 */
	public void addItem2List(E _newData) {
		synchronized (mLock) {
			_datas.add(_newData);
			notifyDataSetChanged();
		}
	}

	/**
	 * @Title: removeIitemFromList
	 * @Description: 删除当前数据
	 *            单个对象数据
	 * @throws
	 */
	public void removeIitemFromList(E _newDataItem) {
		synchronized (mLock) {
			_datas.remove(_newDataItem);
			notifyDataSetChanged();
		}
	}

	/**
	 * @Title: removeIitemFromList
	 * @Description: 删除当前数据
	 *            单个对象数据
	 * @throws
	 */
	public void removeAllData() {
		synchronized (mLock) {
			_datas.clear();
			notifyDataSetChanged();
		}
	}

	/**
	* @Title: addAll
	* @Description: 添加新数据  
	* @throws
	*/
	public void addAll(List<E> _newDatas) {
		synchronized (mLock) {
			_datas.addAll(_newDatas);
			notifyDataSetChanged();
		}
	}

	public void setData(List<E> _newDatas){
		this._datas=_newDatas;
		notifyDataSetChanged();
	}

	public Context get_context() {
		return _context;
	}

	public void set_context(Context _context) {
		this._context = _context;
	}

	/**
	 * @MethodthName: get_datas
	 * @Description: 返回数据
	 * @params: []
	 * @Return:java.util.List<E>
	 * @Throw:
	 */
	public List<E> get_datas() {
		return _datas;
	}

}
